package com.leetcode;

/**
 * SudokuValid..
 *
 * @author Lizhong
 * @date 2019/9/4
 */
public class SudokuValid {
    class Solution {

        public boolean isValidSudoku02(char[][] board) {
            // 0 ,3 , 6
            // 0 , 3, 6
            int[] q = null;
            int[] w = null;
            int[] e = null;

            for (int i = 0; i < 9; i++) {

                int[] x = new int[9];
                int[] y = new int[9];


                if (i % 3 == 0) {
                    q = new int[9];
                    w = new int[9];
                    e = new int[9];
                }
                for (int j = 0; j < 9; j++) {

                    if (!tt(board[i][j], x)) return false;
                    if (!tt(board[j][i], y)) return false;
                    // 判断方格
                    // int boxNum = i / 3 * 3 + j / 3;
                    if (j < 3) {
                        if (!tt(board[i][j], q)) return false;
                    } else if (j < 6) {
                        if (!tt(board[i][j], w)) return false;

                    } else {
                        if (!tt(board[i][j], e)) return false;
                    }

                }
            }
            return true;

        }

        public boolean isValidSudoku01(char[][] board) {
            // 每一位记录一行是否重复
            int x[] = new int[9];
            int y[] = new int[9];
            int z[] = new int[9];
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    char c = board[i][j];
                    if ('.' == board[i][j]) {
                        continue;
                    }
                    // 从1.2.3....
                    int t = c - 48;
                    // 核心思想 一位%2
                    if ((x[i] >> t) % 2 == 1) {
                        return false;
                    } else {
                        // 加1再移回去
                        x[i] += 1 << t;
                    }

                    if ((y[j] >> t) % 2 == 1) {
                        return false;
                    } else {
                        y[j] += 1 << t;
                    }

                    // 判断一个方格，有点东西 i/3 *3
                    int m = i / 3 * 3 + j / 3;
                    if ((z[m] >> t) % 2 == 1) {
                        return false;
                    } else {
                        z[m] += 1 << t;
                    }
                }
            }
            return true;

        }


        public boolean isValidSudoku(char[][] board) {
            // 0 ,3 , 6
            // 0 , 3, 6
            for (int i = 0; i < 9; i++) {

                int[] x = new int[9];
                int[] y = new int[9];
                for (int j = 0; j < 9; j++) {

                    if (!tt(board[i][j], x)) return false;
                    if (!tt(board[j][i], y)) return false;
                    // 判断方格
                    if (i % 3 == 0 && j % 3 == 0) {
                        int[] temp = new int[9];
                        for (int m = 0; m < 3; m++) {
                            for (int n = 0; n < 3; n++) {
                                if (!tt(board[i + m][j + n], temp)) return false;
                            }
                        }
                    }
                }

            }
            return true;

        }

        boolean tt(char c, int[] temp) {
            // 49-57
            if (c != '.') {
                int k = c;
                if (temp[k - 49] != 0)
                    return false;
                else
                    temp[k - 49] = 1;
            }
            return true;
        }


    }
}
